/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 */

import { FileSet } from '@ohos/hvigor';
import { ProjectConfig } from '@ohos/hvigor-arkts-compose';
import fse from 'fs-extra';

import { CodeType } from '../../enum/code-type-enum.js';
import { LegacyAbilityModel } from '../../model/ability/legacy-ability-model.js';
import { TaskNames } from '../common/task-names.js';
import { TargetTaskService } from '../service/target-task-service.js';

import { LegacyArkCompile } from './legacy-ark-compile.js';
import { CommonConst } from '../../const/common-const.js';

/**
 * 处理ohosTest Target时调用loader编译老模型的js/ets源码
 *
 * @since 2023/1/28
 */
export class LegacyOhosTestArkCompile extends LegacyArkCompile {
  constructor(taskService: TargetTaskService, codeType: CodeType) {
    super(taskService, codeType, TaskNames.LegacyFATask.OHOS_TEST_ARK_COMPILE);
  }

  declareInputFiles(): FileSet {
    const fileSet = super.declareInputFiles();

    // ohosTest引用了main中的代码时，main源码也要作为编译增量条件
    if (this.mainCodePath && fse.existsSync(this.mainCodePath!)) {
      fileSet.addEntry(this.mainCodePath!, { isDirectory: true });
    }
    return fileSet;
  }

  protected initDefaultLegacyArkConfig(rOS: string, sInfo: string, abilityInfo: LegacyAbilityModel): ProjectConfig {
    const projectConfig = {
      ...super.initDefaultLegacyArkConfig(rOS, sInfo, abilityInfo),
      supportChunks: false,
    };

    if (projectConfig.testFrameworkPar) {
      projectConfig.testFrameworkPar.testMode = CommonConst.OHOS_TEST;
    }
    return projectConfig;
  }
}
